package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseVO;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.atguigu.jxc.service.ReturnListService;
import com.google.gson.Gson;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WZhaokang
 * @date 2020/12/29 16:09
 */
@RestController
public class ReturnListGoodsController {

    @Autowired
    private ReturnListGoodsService returnListGoodsService;
    @Autowired
    private ReturnListService returnListService;

    //http://localhost:8080/returnListGoods/save?returnNumber=TH1605768591211（退货单号前端生成）
    @RequestMapping("/returnListGoods/save")
    public ServiceVO returnListGoods(ReturnList returnList, String returnListGoodsStr, HttpSession session){
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();
        returnList.setUserId(userId);
        int res = returnListGoodsService.save(returnList, returnListGoodsStr);
        if(res > 0){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
        }
        return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.PARA_TYPE_ERROR_MESS,null);
    }


    //请求URL：http://localhost:8080/returnListGoods/list
    @RequestMapping("/returnListGoods/list")
    public Map<String, List<ReturnList>> returnListGoods(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime){
        Map map = new HashMap();
        List<ReturnList> returnLists = returnListService.returnListGoodsList(returnNumber,supplierId,state,sTime,eTime);
        map.put("rows",returnLists);
        return map;
    }

    //请求URL：http://localhost:8080/returnListGoods/goodsList
    @RequestMapping("/returnListGoods/goodsList")
    public Map<String, List<ReturnListGoods>> goodsList(Integer returnListId){
        Map map = new HashMap();
        List<ReturnListGoods> returnListGoodsList = returnListGoodsService.goodList(returnListId);
        map.put("rows",returnListGoodsList);
        return map;
    }

    //请求URL：http://localhost:8080/returnListGoods/delete
    @RequestMapping("returnListGoods/delete")
    public ServiceVO delete(Integer returnListId){
        returnListService.delete(returnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    //请求URL：http://localhost:8080/returnListGoods/count
    @RequestMapping("/returnListGoods/count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        List<PurchaseVO> purchaseVOList = returnListService.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String json = gson.toJson(purchaseVOList);
        return json;
    }

}
